package cn.bytecollege.controller;

import cn.bytecollege.model.User;
import cn.bytecollege.service.UserService;
import cn.bytecollege.vo.UserVO;
import cn.hutool.crypto.SecureUtil;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Log
@Controller
@RequestMapping("/ourProject")
public class LoginController {
    @Autowired
    private UserService userService;
    @GetMapping("/index")
    public String index() {
        return "index";
    }
    @GetMapping("/index2")
    public String index2() {
        return "index2";
    }
    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, Model model, UserVO userVO) {
        //验证图片验证码（成功跳转主页，失败重新输入）
        // 用户前端页面输入的验证码
        String validCode = request.getParameter("validCode");
        // ValidCodeController生成的验证码
        String code = (String) session.getAttribute("code");
        if(!validCode.equals(code)) {
            model.addAttribute("msg", "验证码错误！！");
            return "login";
        }

        //根据账号获取密码
        User user = userService.getUser(userVO.getUsername());
        //加密用户输入的密码
        String password = SecureUtil.md5(userVO.getPassword());
        //判断是否输入账号和密码
        if(userVO == null) {
            model.addAttribute("msg", "该用户不存在！！");
            return "login";
        }

        //判断密码是否一致
        if(!user.getPassword().equals(password)) {
            model.addAttribute("msg", "密码错误！！");
            return "login";
        }
        //封装查询到的用户user
        session.setAttribute("user", user);

        if(user.getPower().equals("管理员") || user.getPower().equals("社区人员")) {
            return "redirect:/goods/setup";
        }
        if(user.getPower().equals("配送人员")) {
            return "redirect:/HQ/riderOrder";
        }

        return "index2";
    }

    @GetMapping("/register")
    public String addUser() {
        return "register";
    }
    @PostMapping("/register")
    public String addUser(UserVO userVO, Model model, HttpServletRequest request, HttpSession session) {
        // 获取自动生成的验证码 和 用户输入的验证码
        String mailValid = request.getParameter("mailValid");
        String validCode = (String) session.getAttribute("validCode");

        // 判断是否输入验证码   验证码是否一致
        if (mailValid.length() <= 0) {
            model.addAttribute("msg", "请填写验证码！！");
            return "forget";
        }
        if(!mailValid.equals(validCode)){
            model.addAttribute("msg", "验证码有误，请重新输入！！");
            return "register";
        }

        //判断输入密码是否合法
        if(userVO.getPassword().length() < 8) {
            model.addAttribute("msg", "密码必须为8~16位！！");
            return "register";
        }

        //封装数据进对象，添加对象
        String password = SecureUtil.md5(userVO.getPassword());
        User user = User.builder()
                .username(userVO.getUsername())
                .password(password)
                .build();
        userService.addUser(user);

        return "redirect:/ourProject/login";
    }
    @GetMapping("/forget")
    public String modUser() {
        return "forget";
    }
    @PostMapping("/forget")
    public String modUser(UserVO userVO, Model model, HttpServletRequest request, HttpSession session) {

        User user1 = userService.getUser(userVO.getUsername());
        if(user1 == null) {
            model.addAttribute("msg", "该用户不存在！！");
            return "forget";
        }

        String mailValid = request.getParameter("mailValid");
        String validCode = (String) session.getAttribute("validCode");
        if (mailValid.length() <= 0) {
            model.addAttribute("msg", "请填写验证码！！");
            return "forget";
        }
        if(!mailValid.equals(validCode)){
            model.addAttribute("msg", "邮箱验证码有误！！");
            return "forget";
        }

        if(userVO.getPassword().length() < 8) {
            model.addAttribute("msg", "密码为8~16位！！");
            return "forget";
        }

        String password = SecureUtil.md5(userVO.getPassword());
        User user = User.builder()
                .userId(user1.getUserId())
                .username(userVO.getUsername())
                .password(password)
                .build();
        userService.modUser(user);

        return "redirect:/ourProject/login";
    }

    @GetMapping("/personal")
    public String p() {
        return "personal";
    }

    @GetMapping("/b")
    public String a(Model model){
        model.addAttribute("msg", "chifbvjn");
        return "search";
    }
}
